package cn.wolfcode.service.Impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void save(Role role,Long[]ids) {
        roleMapper.insert(role);
        if (ids!=null&&ids.length>0){
            for (Long id : ids) {
                roleMapper.insertRelation(role.getId(),id);
            }

        }


    }

    @Override
    public void delete(Long id) {
            roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Role role,Long[]ids) {
        roleMapper.updateByPrimaryKey(role);
        roleMapper.deleteRelation(role.getId());
        if (ids!=null&&ids.length>0){
            for (Long id : ids) {
                roleMapper.insertRelation(role.getId(),id);
            }
        }
    }

    @Override
    public Role get(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }

    @Override
    public PageInfo<Role> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Role> list = roleMapper.queryForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public List<Permission> selectByRoleId(Long id) {
        List<Permission> permissions = roleMapper.selectByRoleId(id);
        return permissions;
    }

    @Override
    public List<Role> selectByEmpId(Long id) {
         List<Role>list=roleMapper.selectByEmpId(id);
        return list;
    }
}
